package com.xcw.utils;

import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @author eagle
 *
 */
public class Bean2MapUtil {
	/**
	 * bean转map
	 */
	public static Map<String, Object> beanToMap(Object bean, String... columns) {
		if (bean == null) {
			return new HashMap<String, Object>();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		BeanMap beanMap = BeanMap.create(bean);
		if(columns==null || columns.length==0){
			for (Object key : beanMap.keySet()) {
				map.put(key + "", beanMap.get(key));
			}
		}else{
			for(String column:columns){
				if(beanMap.containsKey(column)){
					map.put(column, beanMap.get(column));
				}
			}
		}
		return map;
	}
}
